import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2023-02-11
 * Time: 10:08
 */
public class Day16 {
    //389.找不同
    public char findTheDifference(String s, String t) {
        char[] ret1 = s.toCharArray();
        char[] ret2 = t.toCharArray();

        int sum = 0;
        for (int i = 0; i < ret1.length; i++) {
            sum = sum ^ ret2[i] ^ ret1[i];
        }

        return (char) (sum ^ ret2[ret2.length - 1]);
    }
    //392.判断子序列
    public boolean isSubsequence(String s, String t) {
        char[] ss = s.toCharArray();
        char[] ts = t.toCharArray();

        int i = 0, j = 0;
        while (i < ss.length && j < ts.length){
            if (ss[i] == ts[j]){
                i++;
            }
            j++;
        }
        return i == ss.length;
    }
    //409.最长回文串
    public int longestPalindrome(String s) {
        char[] ret = new char[58];

        char[] ss = s.toCharArray();
        for (int i = 0; i < ss.length; i++) {
            ret[ss[i] - 65]++;
        }
        int sum = 0;
        int flag = 0;
        for (int i = 0; i < ret.length; i++) {
            sum += ret[i];
            if (ret[i] % 2 == 1){
                flag++;
            }
        }
        if (flag > 0){
            flag--;
            sum -= flag;
        }
        return sum;
    }
    //412.Fizz Buzz
    public List<String> fizzBuzz(int n) {
        List<String> list = new ArrayList<>();

        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 == 0){
                list.add("FizzBuzz");
            }else{
                if (i % 3 == 0){
                    list.add("Fizz");
                }else if (i % 5 == 0){
                    list.add("Buzz");
                }else{
                    list.add(i + "");
                }
            }
        }
        return list;
    }
    //415.字符串
    public String addStrings(String num1, String num2) {
        StringBuilder sb = new StringBuilder();

        char[] num1s = num1.toCharArray();
        char[] num2s = num2.toCharArray();

        int i = num1s.length - 1, j = num2s.length - 1;
        int ret = 0;
        while (i >= 0 || j >= 0 || ret != 0){
            if (i >= 0) ret += num1s[i--] - '0';
            if (j >= 0) ret += num2s[j--] - '0';
            sb.append(ret % 10);
            ret /= 10;
        }
        return sb.reverse().toString();
    }
    //434.字符串中的单词数
    public int countSegments(String s) {
        char[] ss = s.toCharArray();

        int ret = 0;
        for (int i = 0; i < ss.length - 1; i++) {
            if (ss[i] != ' ' && ss[i + 1] == ' '){
                ret++;
            }
            while (i < ss.length - 1 && ss[i + 1] == ' '){
                i++;
            }
        }
        if (ss.length >= 1 && ss[ss.length - 1] != ' '){
            ret++;
        }
        return ret;
    }
}
